package cn.johnyu.controller;

import cn.johnyu.domain.Book;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class BookController {
    private static List<Book> books= new ArrayList<>(Arrays.asList(new Book(1,"java"),new Book(2,"python")));
    private static int index=2;
    @GetMapping("/books")
    public List<Book> findAll(){
        return books;
    }

    @PostMapping("/books")
    public Book addBook(@RequestBody Book book){
        System.out.println("working..."+book);
        book.setId(++index);
        books.add(book);
        return book;
    }


}
